#include <iostream>
#include "FileCat.h"
using namespace std;

void ParseCmd(const CStringA& strUserInput, CStringA& strCmd, CStringA& strValue)
{
	strCmd.Empty();
	strValue.Empty();
	int iSep = strUserInput.Find("=");
	if (-1 == iSep)
	{
		return;
	}

	strCmd.Append(strUserInput, iSep);

	int iEndBegin = iSep + 1;
	strValue.Append((LPCSTR)strUserInput + iEndBegin, strUserInput.GetLength() - iEndBegin);
	return;
}

void ScanCmd(CFileCat& FileCat, int argc, char** argv)
{
	for (int i = 1; i < argc; i++)
	{
		CStringA strCmd;
		CStringA strValue;

		CStringA strUserInput = argv[i];
		ParseCmd(strUserInput, strCmd, strValue);

		if (!strValue.IsEmpty())
		{
			FileCat.AddCmd(strCmd, strValue);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
	CFileCat FileCat;
	FileCat.InitCmdSet();
	ScanCmd(FileCat, argc, argv);

	bool bProcess = FileCat.DoProcess();
	if (!bProcess)
	{
		cout << "process file error!" << endl;
	}

	bProcess = FileCat.DoProcessEnd();
	if (!bProcess)
	{
		cout << "cannot close output file!" << endl;
	}

	cout << "cat file ok!" << endl;
	return 0;
}